package cn.org.tpeach.nosql.controller;

import cn.org.tpeach.nosql.enums.RedisTreeType;


import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import lombok.Getter;

import java.net.URL;
import java.util.ResourceBundle;
@Getter
public class AddKeyController implements Initializable {
    @FXML
    private GridPane addKeyGridPane;
    @FXML
    private TextField addKeyNameField;
    @FXML
    private TextField addKeyTtlField;
    @FXML
    private TextField addKeyScoreOrField;
    @FXML
    private ComboBox<RedisTreeType> addKeyComboType;
    @FXML
    private TextArea addKeyValueTextArea;
    @FXML
    private Text addKeyScoreOrFieldText;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addKeyComboType.getItems().addAll(RedisTreeType.STRING,RedisTreeType.LIST,RedisTreeType.SET,RedisTreeType.HASH,RedisTreeType.ZSET);
        addKeyComboType.setValue(RedisTreeType.STRING);
        changeTypeListener(RedisTreeType.STRING);
        addKeyNameField.widthProperty().addListener((observable, oldValue, newValue) ->addKeyComboType.setMinWidth(newValue.doubleValue()));
        addKeyComboType.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> changeTypeListener(newValue));
    }

    private void changeTypeListener(RedisTreeType newValue){
        boolean isVisitScoreOrField = true;
        if(RedisTreeType.HASH.equals(newValue)){
            addKeyScoreOrField.setPromptText("Field");
            addKeyScoreOrFieldText.setText("数据散列键");
            addKeyGridPane.getChildren().remove(addKeyValueTextArea);
            addKeyGridPane.add(addKeyValueTextArea,1,4,5,4);
        }else if(RedisTreeType.ZSET.equals(newValue)){
            addKeyScoreOrFieldText.setText("数据分数");
            addKeyScoreOrField.setPromptText("Score");
            addKeyGridPane.getChildren().remove(addKeyValueTextArea);
            addKeyGridPane.add(addKeyValueTextArea,1,4,5,4);
        }else{
            addKeyGridPane.getChildren().remove(addKeyValueTextArea);
            addKeyGridPane.add(addKeyValueTextArea,1,3,5,5);
            isVisitScoreOrField = false;
        }
        addKeyScoreOrField.setVisible(isVisitScoreOrField);
        addKeyScoreOrField.setManaged(isVisitScoreOrField);
        addKeyScoreOrFieldText.setVisible(isVisitScoreOrField);
        addKeyScoreOrFieldText.setManaged(isVisitScoreOrField);
    }
}
